package com.back.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.base.dao.BaseDao;
import com.base.dao.IBaseDao;
import com.domain.ActivityTable;
import com.domain.SchoolPhoto;
import com.util.comm.DateTimeUtil;

public class SchoolPhotoServiceImpl implements ISchoolPhotoService {
	private static final Logger LOGGER = Logger.getLogger(SchoolPhotoServiceImpl.class);
	 private IBaseDao<SchoolPhoto, Long>  dao ;
	 public  SchoolPhotoServiceImpl(){
		 dao  =  new BaseDao<SchoolPhoto, Long>(SchoolPhoto.class);
	 }
	public boolean saveOrUpdateSchoolPhoto(SchoolPhoto photo) {
	  try{
		   dao.saveOrUpdate(photo);
		   return true;
	  }catch (Exception e) {
		  LOGGER.error(" saveOrUpdateSchoolPhoto error", e);
		  return false;
	}
	}
	public  List<SchoolPhoto>  findByParameMap(Map<String,String> parameMap){
		 try{
			 String str = " SELECT  * FROM schoolphoto where 1=1 ";
			 if(null!=parameMap.get("lanType"))
				{
					str+=" and lanType= "+parameMap.get("lanType");
				}
			       str+="  ORDER  BY creatTime DESC ";
			 if(parameMap.get("page") != null  &&  parameMap.get("limit") !=null )
				{
					 int start=Integer.parseInt(parameMap.get("page"))-1;
					 int limit =Integer.parseInt(parameMap.get("rows"))-1;
					 str +=" limit "+start*limit+","+limit ;
				}
			 
			 List<SchoolPhoto> list =   dao.findList(str) ;
			   if (list !=null  && !list.isEmpty())
			   {
				   List<SchoolPhoto> newlist =  new ArrayList<SchoolPhoto>();
				   for(SchoolPhoto schoolPhoto : list)
				   {
					   schoolPhoto.setCreatTimeStr
					   (DateTimeUtil.switchDateToString(schoolPhoto.getCreatTime(),"yyyy-MM-dd HH:mm:ss"));
					   newlist.add(schoolPhoto);
				   }
				   return  newlist;
			   }
		 }catch (Exception e) {
			 LOGGER.error(" SchoolPhotoServiceImpl findByParameMap error", e);;
			 return null;
		}
		 return null;
	}
	 public   boolean   deleteById(String ids){
		 boolean isFlag = true;
			if ("".equals(ids))
				return false;
			String sql = "";
			String[] delteid = ids.split("_");
			try {
				for (int i = 0; i < delteid.length; i++) {
					sql = "delete  from schoolphoto where  id=" + delteid[i]  ;		
					dao.delete(sql);
				}
			} catch (Exception e) {
				e.printStackTrace(); 
				LOGGER.error(" SchoolPhotoServiceImpl deleteById   error", e);
				return false;
			}
			return isFlag;
	 }
	public   int count (Map<String,String> parameMap){
		try{
			String  str="  SELECT  * FROM schoolphoto  ";
		    List<SchoolPhoto> list=dao.findList(str) ;
		    if(list!=null )
		    {
		    	return list.size();
		    }
		}catch (Exception e) {
			LOGGER.error(" ActivityTable  count error", e);
		    return 0;
	}
		return 0;
	}
	 public   SchoolPhoto   findById(String id){
		 String str = " SELECT  * FROM schoolphoto  WHERE id =? ";
			try{
				List<SchoolPhoto>	list = dao.findList(str, new Object[]{id});
				  if(list!=null  &&  !list.isEmpty())
				    {
				    	return list.get(0);
				    }
			}catch(Exception e){
				LOGGER.error(" SchoolPhotoServiceImpl  findById "+id +" error", e);
			    return null;
			}
			
			return null;
		}
	 public   List<SchoolPhoto>  findBySchoolList(Map<String,String> parameMap){
		  try{
				String  str="  SELECT * FROM  schoolphoto WHERE  1=1   ";
				if(null!=parameMap.get("type"))
				{
					str+=" and type= "+parameMap.get("type");
				}
				if(null!=parameMap.get("lanType"))
				{
					str+=" and lanType= "+parameMap.get("lanType");
				}
				if(null!=parameMap.get("id"))
				{
                  str+=" and id= " + parameMap.get("id");				
				}
				str+="  ORDER  BY id DESC ";
				if(parameMap.get("page") != null  &&  parameMap.get("limit") !=null )
				{
					 int start=Integer.parseInt(parameMap.get("page"))-1;
					 int limit =Integer.parseInt(parameMap.get("limit"));
					 str +=" limit "+start*limit+","+limit ;
				}
			   List<SchoolPhoto>  list = dao.findList(str);
			   return list;
			   
			}catch (Exception e) {
				LOGGER.error(" ActivityTable  findByMap error", e);
			    return null;
		  }
			
	  }
}
